# Derived from https://github.com/eclarson/MachineLearningNotebooks/blob/master/10.%20Keras%20Wide%20and%20D
from
sklearn
import
metrics
as
mt
import
tensorflow
as
tf
from
tensorflow
import
keras
import
os
import
numpy
as
np
os.environ['AUTOGRAPH_VERBOSITY'] = '0'
from
tensorflow.keras.layers
import
Dense, Activation, Input, concatenate
from
tensorflow.keras.models
import
Model
from
tensorflow.keras.wrappers.scikit_learn
import
KerasRegressor
from
sklearn.model_selection
import
KFold, cross_val_score
# Define the input size
num_features = X_train.shape[1]
input_tensor = Input(shape=(num_features,))
#Creating the 3 combined wide/deep networks
# Define the wide branch
wide_branch = Dense(units=5, activation='relu')(input_tensor)
# Define the first deep branch
deep_branch_1 = Dense(units=5, activation='relu')(input_tensor)
# Define the second deep branch
deep_branch_2 = Dense(units=5, activation='relu')(input_tensor)
# Merge the branches together
merged = concatenate([wide_branch, deep_branch_1, deep_branch_2])
# Define the output layer
predictions = Dense(1, activation='relu')(merged)
# Define a function that returns the compiled model
def
create_model():
model = Model(inputs=input_tensor, outputs=predictions)
model.compile(optimizer='sgd', loss='mae')
return
model
# Wrap the Keras model in a scikit-learn estimator
estimator = KerasRegressor(build_fn=create_model, epochs=10, batch_size=50, verbose=1)
# Define k-fold cross-validation
kf = KFold(n_splits=3)
# Train and evaluate the model using k-fold cross-validation
mae_scores = -1 * cross_val_score(estimator, X_train, y_train, cv=kf, scoring='neg_mean_absolute_error')
mape_scores = -1 * cross_val_score(estimator, X_train, y_train, cv=kf, scoring='neg_mean_absolute_percentage
# Fit the model on the full training set
estimator.fit(X_train, y_train)
# Evaluate the model on the test set
yhat = estimator.predict(X_test)
print
('Mean Absolute Error:', mt.mean_absolute_error(y_test, yhat))
print
('Mean Absolute Percentage Error:', mt.mean_absolute_percentage_error(y_test, yhat))
print
('Average MAE:', np.mean(mae_scores))
print
('Average MAPE:', np.mean(mape_scores))